package edu.nf.hotel.roominfodao.impl;

import edu.nf.hotel.roominfo.RoomInfo;
import edu.nf.hotel.roominfo.RoomInfoDao;
import edu.nf.hotel.roominfo.all.CityRoom;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-06-25
 * @Author 带土
 */
public class RoomInfoDaoImpl implements RoomInfoDao {

    @Override
    public void getAddRoomInfo(RoomInfo r) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "insert into room_info(rt_id,ht_id,room_no,room_name,room_facilities,room_price,remark)values(?,?,?,?,?,?,?)";
        executor.executeUpdate(sql,r.getRtId(),r.getHtId(),r.getRoomNo(),r.getRoomName(),r.getRoomFacilities(),r.getRoomPrice(),r.getReMark());
    }

    @Override
    public void getDeleteRoomInfo(int roomId) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "delete from room_info where room_id=?";
        executor.executeUpdate(sql,roomId);
    }

    @Override
    public void getUpdateRoomInfo(RoomInfo r) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "update room_info set rt_id=?,ht_id=?,room_no=?,room_name=?,room_facilities=?,room_price=?,remark=? where room_id=?";
        executor.executeUpdate(sql,r.getRtId(),r.getHtId(),r.getRoomNo(),r.getRoomName(),r.getRoomFacilities(),r.getRoomPrice(),r.getReMark(),r.getRoomId());
    }

    /**
     * 查询客房全部数据
     * @return
     */
    @Override
    public List<RoomInfo> getAllRoomInfo() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<RoomInfo> handler = new BeanListHandler<>(RoomInfo.class);
        String sql = "select room_id,room_no,room_name,room_image,room_facilities,room_price,remark from room_info";
        List<RoomInfo> list = executor.executeQuery(sql, handler);
        list.forEach(roomInfo -> System.out.println(roomInfo.getRoomName()+roomInfo.getRoomPrice()));
        return list;
    }

    @Override
    public List<CityRoom> getAllName(String cityName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select city_id,city_code,city_name,city_scape_image,room_id,rt_id,room_no,room_name,room_image," +
                "room_facilities,room_price,remark from city_type join room_info ri on city_id = ri.ht_id where city_name =?" +
                "group by city_id,city_code,city_name,city_scape_image,room_id,rt_id,room_no,room_name,room_image," +
                "room_facilities,room_price,remark";
        BeanListHandler<CityRoom> handler = new BeanListHandler<>(CityRoom.class);
        List<CityRoom> list = executor.executeQuery(sql, handler, cityName);
        list.forEach(cityRoom -> System.out.println(cityRoom.getRoomPrice()));
        return list;
    }


}
